/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.sensor.cropcircle;
import java.text.ParseException;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.EnumMap;
import nl.cloudfarming.client.model.SensorDataFile;
import org.openide.util.NbBundle;

/**
 *
 * @author Gerben Feenstra
 */
public class CropcircleASC340FileProcessor extends CropcircleFileProcessor {
    

    private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HHmmss.SS");
    private final String LOGKEY_TYPE_DISPLAYNAME = NbBundle.getMessage(this.getClass(), "cropcircle.fileprocessor.asc340.log.displayname");

    public CropcircleASC340FileProcessor(File toProcess) {
        super(toProcess);
    }

    /**
     * Parses payload to CropcircleSensorData
     * @param payload Cropcircle ASC340 sensor line
     * @param dataFile
     * @return CropcircleSensorData object
     * @throws ParseException
     * @throws CropcircleParseException
     */
    @Override
    public CropcircleSensorData parse(String payload, SensorDataFile dataFile) throws CropcircleParseException {
        // First create a entity representation of a file
        //converting the differrent data
        EnumMap map = new EnumMap<CropcircleASC340FileDescription, Object>(CropcircleASC340FileDescription.class);
        String[] fields = payload.split(CropcircleASC340FileDescription.getDelimiter());

        try {
            for (CropcircleASC340FileDescription c : CropcircleASC340FileDescription.values()) {
                if (c.isMandatory()) {
                    if (c.getTheClass().equals(java.lang.Double.class)) {
                        double theDouble = Double.parseDouble(fields[c.getPosition()]);
                        map.put(c, theDouble);
                    } else if (c.getTheClass().equals(java.lang.Integer.class)) {
                        int theInt = Integer.parseInt(fields[c.getPosition()]);
                        map.put(c, theInt);
                    } else if (c.getTheClass().equals(java.util.Date.class)) {
                        Date theDate = TIME_FORMAT.parse(fields[c.getPosition()]);
                        map.put(c, theDate);
                    } else {
                        map.put(c, fields[c.getPosition()]);
                    }
                }
            }
        } catch (ParseException e) {
            throw new CropcircleParseException("Error parsing date ", e);
        } catch (Exception e) {
            throw new CropcircleParseException(e);
        }

        CropcircleSensorData sensorData = new CropcircleSensorData();
        sensorData.setDataSensorFile(dataFile);
        sensorData.setLongitude((Double) map.get(CropcircleASC340FileDescription.LONGITUDE));
        sensorData.setLatitude((Double) map.get(CropcircleASC340FileDescription.LATITUDE));
        sensorData.setSpeed((Double) map.get(CropcircleASC340FileDescription.SPEED));
        sensorData.setCourse((Double) map.get(CropcircleASC340FileDescription.COURSE));
        sensorData.setSensor((Double) map.get(CropcircleASC340FileDescription.SENSOR));
        sensorData.setVi1((Double) map.get(CropcircleASC340FileDescription.VI1));
        sensorData.setVi2((Double) map.get(CropcircleASC340FileDescription.VI2));
        sensorData.setR1((Double) map.get(CropcircleASC340FileDescription.R1));
        sensorData.setR2((Double) map.get(CropcircleASC340FileDescription.R2));
        sensorData.setR3((Double) map.get(CropcircleASC340FileDescription.R3));
        sensorData.setAppRate((String) map.get(CropcircleASC340FileDescription.APP_RATE));
        return sensorData;
    }
    
    /**
     * Concrete subclasses must override this method to return the displayname of that sensorprocessor to be displayed in the log.
     * For example: "ASC-340 Cropcircle"
     * 
     * Primary use is for logging, for example: [Info] Start processing of ASC-340 Cropcircle sensor file 010710AD.CSV
     * 
     * @return 
     */
    @Override
    protected String getLogDisplayName() {
        return LOGKEY_TYPE_DISPLAYNAME;
    }
}
